package com.qczl.pme.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qczl.pme.controller.phone.vo.AreaVo;
import com.qczl.pme.controller.phone.vo.LiveVo;
import com.qczl.pme.controller.phone.vo.PersonVo;
import com.qczl.pme.controller.phone.vo.SignVo;
import com.qczl.pme.controller.phone.vo.StbVo;
import com.qczl.pme.controller.phone.vo.TopicVo;
import com.qczl.pme.controller.phone.vo.VideoVo;
import com.qczl.pme.dao.AreaCmsDao;
import com.qczl.pme.dao.AreaDao;
import com.qczl.pme.dao.CameraDao;
import com.qczl.pme.dao.LiveDao;
import com.qczl.pme.dao.LiveVideoDao;
import com.qczl.pme.dao.MettingDao;
import com.qczl.pme.dao.PersonDao;
import com.qczl.pme.dao.SignDao;
import com.qczl.pme.dao.StbDao;
import com.qczl.pme.dao.TopicDao;
import com.qczl.pme.dao.VideoDao;
import com.qczl.pme.entity.PMEArea;
import com.qczl.pme.entity.PMEAreaCms;
import com.qczl.pme.entity.PMECamera;
import com.qczl.pme.entity.PMELive;
import com.qczl.pme.entity.PMELiveVideo;
import com.qczl.pme.entity.PMEMetting;
import com.qczl.pme.entity.PMEPerson;
import com.qczl.pme.entity.PMESTB;
import com.qczl.pme.entity.PMESign;
import com.qczl.pme.entity.PMETopic;
import com.qczl.pme.entity.PMEVideo;
import com.qczl.pme.service.PhoneService;
import com.qczl.pme.vo.CameraVo;
import com.qczl.pme.vo.MettingVo;
import com.qczl.util.DateUtil;

/**
 * 手机接口业务逻辑层实现类
 * 
 * @author SongR
 * @日期:2017年9月26日上午11:23:57
 */
@Service
public class PhoneServiceImpl implements PhoneService {

	@Autowired
	PersonDao persondao;
	@Autowired
	AreaDao areadao;
	@Autowired
	LiveDao livedao;
	@Autowired
	StbDao stbdao;
	@Autowired
	CameraDao cameradao;
	@Autowired
	TopicDao topicdao;
	@Autowired
	LiveVideoDao lvdao;
	@Autowired
	SignDao signdao;
	@Autowired
	VideoDao videodao;
	@Autowired
	MettingDao dao;
	@Autowired
	AreaCmsDao acdao;

	/**
	 * 实现获取人员区域级别
	 */
	@Override
	public PersonVo getAreaByPersonV1(String phone) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		PersonVo vo = new PersonVo();
		vo.setId(person.getId());
		vo.setName(person.getName());
		vo.setIdCard(person.getIdCard());
		vo.setImg("/resource/person/" + person.getIdCard() + ".png");
		vo.setAreaId(area.getId());
		vo.setAreaName(area.getName());
		vo.setAreaLevel(area.getLevel());
		return vo;
	}

	/**
	 * 实现获取直播列表
	 */
	@Override
	public List<LiveVo> getLiveV1(String phone, String start, String size) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		// 获取直播中的数据
		String hql = "from PMELive where status=1"; 
		List<PMESTB> stbs = new ArrayList<PMESTB>();
		// 县
		if (area.getLevel() == 0) {
			List<PMEArea> areas = areadao.find("from PMEArea where pid=" + area.getId() + " or id=" + area.getId());
			for (PMEArea a : areas) {
				PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
				stbs.add(s);
			}
			// 镇
		} else if (area.getLevel() == 1) {
			List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getPid() + " or id=" + area.getId());
			for (PMEArea a : areas) {
				PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
				stbs.add(s);
			}
		} else if (area.getLevel() == 2) {

		}
		List<PMELive> lives = new ArrayList<PMELive>();
		if (size == null || "".equals(size)) {
			lives = livedao.find(hql);
		} else {
			lives = livedao.findP(hql, Integer.parseInt(start), Integer.parseInt(size));
		}
		List<LiveVo> vos = new ArrayList<LiveVo>();
		for (PMELive l : lives) {
			LiveVo vo = new LiveVo();
			PMESTB s = stbdao.get("from PMESTB where no ='" + l.getStbno() + "' and status=0");
			PMETopic topic = topicdao.get(PMETopic.class, new Long(l.getTopicid()));
			PMECamera camera = cameradao.get(PMECamera.class, s.getCameraid());
			vo.setName(topic.getName());
			vo.setUrl(camera.getUrl());
			vo.setStarttime(l.getStarttime());
			System.out.println(camera.getId());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取直播列表数
	 */
	@Override
	public Long countLiveV1(String phone) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		String hql = "select count(*) from PMELive where status=1";
		if (area.getLevel() == 0) {
		} else {
			PMEArea a = areadao.get("from PMEArea where id='" + area.getPid() + "'");
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			hql += " and stbno='" + s.getNo() + "'";
		}
		return livedao.count(hql);
	}

	/**
	 * 实现获取直播类型
	 */
	@Override
	public List<TopicVo> getLiveTopicV1(String start, String size) throws Exception {
		String hql = "from PMETopic where pid=2 and status=0 order by sort";
		List<PMETopic> topic = new ArrayList<PMETopic>();
		if (size == null || "".equals(size)) {
			topic = topicdao.find(hql);
		} else {
			topic = topicdao.findP(hql, Integer.parseInt(start), Integer.parseInt(size));
		}
		List<TopicVo> vos = new ArrayList<TopicVo>();
		for (PMETopic l : topic) {
			TopicVo vo = new TopicVo();
			vo.setId(l.getId());
			vo.setName(l.getName());
			vo.setPid(l.getPid());
			vo.setSort(l.getSort());
			vo.setStatus(l.getStatus());
			vo.setUserid(l.getUserid());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取直播类型数
	 */
	@Override
	public Long countLiveTopicV1() throws Exception {
		return null;
	}

	/**
	 * 实现获取直播回看列表
	 */
	@Override
	public List<LiveVo> getLiveBackV1(String topicid, String start, String size) throws Exception {
		String hql = "from PMELive where status=1 and  topicid=" + topicid;
		List<PMELive> lives = new ArrayList<PMELive>();
		if (start == null || start.equals("")) {
			lives = livedao.find(hql);
		} else {
			lives = livedao.findP(hql, Integer.parseInt(start), Integer.parseInt(size));
		}
		List<LiveVo> vos = new ArrayList<LiveVo>();
		for (PMELive l : lives) {
			LiveVo vo = new LiveVo();
			PMESTB s = stbdao.get("from PMESTB where no ='" + l.getStbno() + "' and status=0");
			PMETopic topic = topicdao.get(PMETopic.class, new Long(l.getTopicid()));
			PMECamera camera = cameradao.get(PMECamera.class, s.getCameraid());
			PMELiveVideo lv = lvdao.get(PMELiveVideo.class, l.getVideoId());
			vo.setName(topic.getName());
			String url = "http://" + camera.getSewise().getIp();
			if (camera.getSewise().getProt2() != null && !"".equals(camera.getSewise().getProt2())) {
				url += ":" + camera.getSewise().getProt2();
			}
			vo.setUrl(url + lv.getPlayUrl());
			vo.setStarttime(lv.getStartTime());
			vo.setEndtime(lv.getEndTime());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 实现获取直播回看列表数
	 */
	@Override
	public Long countLiveBackV1(String topicid) throws Exception {
		return null;
	}

	/**
	 * 实现获取县级会场
	 */
	@Override
	public List<CameraVo> getCountyV1(String phone, String start, String size) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		// List<AreaVo> areaList = new ArrayList<AreaVo>();
		// List<StbVo> stb = new ArrayList<StbVo>();
		StringBuffer sql = new StringBuffer(
				"select a.* from pme_basis_camera a,pme_basis_stb b,pme_basis_stb_camera c,pme_basis_area d where a.camera_id=c.camera_id and b.stb_id=c.stb_id and a.area_id=d.area_id and a.camera_level=0 and a.camera_type=0 and d.area_level=0 ");
		List<CameraVo> vos = new ArrayList<CameraVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			// AreaVo ar = new AreaVo();
			// StbVo sb = new StbVo();
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append("and b.stb_no='" + s.getNo() + "' order by a.camera_sort");
			} else {
				sql.append("order by a.camera_sort");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMECamera> cameras = cameradao.getBySqlL(sql.toString(), Integer.parseInt(start),
					Integer.parseInt(size));
			for (PMECamera c : cameras) {
				CameraVo vo = new CameraVo();
				BeanUtils.copyProperties(c, vo);
				vo.setAreaid(c.getArea().getId());
				vo.setAreaname(c.getArea().getName());
				vos.add(vo);
			}
			/*
			 * sb.setNo(s.getNo()); ar.setId(a.getId());
			 * ar.setName(a.getName()); ar.setDesc(a.getDesc());
			 * ar.setCreattime(a.getCreattime()); stb.add(sb); areaList.add(ar);
			 */
		}
		return vos;
	}

	@Override
	public Long countCountyV1(String phone) throws Exception {
		return null;
	}

	@Override
	public List<AreaVo> getAreaTownV1(String phone, String start, String size) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		StringBuffer sql = new StringBuffer("select * from pme_basis_area area ");
		List<AreaVo> vos = new ArrayList<AreaVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(
						",(select c.area_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
								+ s.getNo()
								+ "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_level =1 UNION select area_id from pme_basis_area where area_id in(select c.area_pid from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
								+ s.getNo()
								+ "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_level =2)) area1 where area.area_id=area1.area_id order by area.area_sort");
			} else {
				sql.append(" order by area.area_sort");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMEArea> areasList = areadao.getBySqlL(sql.toString(), Integer.parseInt(start),
					Integer.parseInt(size));
			for (PMEArea pmeArea : areasList) {
				AreaVo vo = new AreaVo();
				BeanUtils.copyProperties(pmeArea, vo);
				vos.add(vo);
			}
		}
		return vos;
	}

	@Override
	public Long countAreaTownV1(String phone) throws Exception {
		return null;
	}

	@Override
	public List<CameraVo> getVillageByTownV1(String phone, String townid, String start, String size) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		StringBuffer sql = new StringBuffer("SELECT * from pme_basis_camera camera");
		List<CameraVo> vos = new ArrayList<CameraVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(
						" where camera.camera_id IN(select b.camera_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
								+ s.getNo()
								+ "' and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_pid = "
								+ townid
								+ " UNION select  b.camera_id from pme_basis_stb a,pme_basis_camera b,pme_basis_area c,pme_basis_stb_camera d where a.stb_id = d.stb_id and b.camera_id=d.camera_id and b.area_id=c.area_id and a.stb_no='"
								+ s.getNo()
								+ "'  and a.stb_status=0 and b.camera_status=0 and c.area_status=0 and c.area_id = "
								+ townid + ") order by camera.camera_sort");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMECamera> cameras = cameradao.getBySqlL(sql.toString(), Integer.parseInt(start),
					Integer.parseInt(size));
			for (PMECamera c : cameras) {
				CameraVo vo = new CameraVo();
				BeanUtils.copyProperties(c, vo);
				vo.setAreaid(c.getArea().getId());
				vo.setAreaname(c.getArea().getName());
				vos.add(vo);
			}
		}
		return vos;
	}

	@Override
	public Long countVillageByTownV1(String phone, String townid) throws Exception {
		return null;
	}

	@Override
	public List<CameraVo> getBranchV1(String phone, String start, String size) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		StringBuffer sql = new StringBuffer(
				"select a.* from pme_basis_camera a,pme_basis_stb b,pme_basis_stb_camera c where a.camera_id=c.camera_id and b.stb_id=c.stb_id and a.camera_level=0 and a.camera_type=2");
		List<CameraVo> vos = new ArrayList<CameraVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(" and b.stb_no='" + s.getNo() + "' order by a.camera_sort");
			} else {
				sql.append(" order by a.camera_sort");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMECamera> cameras = cameradao.getBySqlL(sql.toString(), Integer.parseInt(start),
					Integer.parseInt(size));
			for (PMECamera c : cameras) {
				CameraVo vo = new CameraVo();
				BeanUtils.copyProperties(c, vo);
				vos.add(vo);
			}
		}
		return vos;
	}

	@Override
	public Long countBranchV1(String phone) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<VideoVo> getMettingBackV1(String phone, String cameraid, String start, String size) throws Exception {
		List<VideoVo> vos = new ArrayList<VideoVo>();
		if (start == null || "".equals(start)) {
			start = "0";
		}
		if (size == null || "".equals(size)) {
			size = "10";
		}
		List<PMEVideo> videos = videodao.findP("from PMEVideo where status=0 and mettingId is not null and camera.id="
				+ cameraid + " order by startTime desc", Integer.parseInt(start), Integer.parseInt(size));
		for (PMEVideo v : videos) {
			VideoVo vo = new VideoVo();
			PMEMetting m = dao.get("from PMEMetting where id=" + v.getMettingId());
			if (null != m) {
				PMECamera camera = cameradao.get("from PMECamera where id=" + m.getCamera().getId());
				String url = "http://" + camera.getSewise().getIp();
				if (camera.getSewise().getProt2() != null && !"".equals(camera.getSewise().getProt2())) {
					url += ":" + camera.getSewise().getProt2();
				}
				vo.setEndtime(DateUtil.dataToStr(v.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
				vo.setStarttime(
						v.getStartTime() == null ? null : DateUtil.dataToStr(v.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
				vo.setPlayurl(url + v.getPlayUrl());
				vo.setName(m.getTopic().getName());
				vos.add(vo);
			}
		}
		return vos;
	}

	@Override
	public Long countMettingBackV1(String phone, String cameraid) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PersonVo> getZuoBanPersonV1(String phone, String start, String size) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		StringBuffer sql = new StringBuffer(
				"select a from PMEPerson a,PMESign b where a.id=b.personid and b.signtypeid=2 and b.state=0 and a.status=0");
		List<PersonVo> voArray = new ArrayList<PersonVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(" and b.stbno='" + s.getNo() + "' order by b.signtime desc");
			} else {
				sql.append(" order by b.signtime desc");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMEPerson> personArray = persondao.findP(sql.toString(), Integer.parseInt(start),
					Integer.parseInt(size));
			for (PMEPerson p : personArray) {
				PersonVo vo = new PersonVo();
				BeanUtils.copyProperties(p, vo);
				vo.setImg("/resource/person/" + p.getIdCard() + ".png");
				voArray.add(vo);
			}
		}
		return voArray;
	}

	@Override
	public Long countZuoBanPersonV1(String phone) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SignVo> getZuoBanSignListV1(String phone, String personid, String start, String size) throws Exception {
		PMEPerson persons = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = persons.getArea();
		StringBuffer sql = new StringBuffer(
				"select a from PMESign a,PMESTB b where a.signtypeid=2 and a.stbno=b.no and a.state!=-1 and b.status=0 and a.personid="
						+ personid + "");
		List<SignVo> vos = new ArrayList<SignVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(" and b.no='" + s.getNo() + "' order by a.signtime desc");
			} else {
				sql.append(" order by a.signtime desc");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMESign> signArray = signdao.findP(sql.toString(), Integer.parseInt(start), Integer.parseInt(size));
			for (PMESign pmeSign : signArray) {
				PMEPerson person = persondao.get(PMEPerson.class, pmeSign.getPersonid());
				PMEVideo video = videodao.get("from PMEVideo where taskId='" + pmeSign.getTaskid() + "'");
				PMESTB stb = stbdao.get("from PMESTB where no = '" + pmeSign.getStbno() + "' and status = 0");
				PMECamera camera = cameradao.get("from PMECamera where id=" + stb.getCameraid());
				String url = "http://" + camera.getSewise().getIp();
				if (camera.getSewise().getProt2() != null && !"".equals(camera.getSewise().getProt2())) {
					url += ":" + camera.getSewise().getProt2();
				}
				SignVo vo = new SignVo();
				vo.setUrl(url + video.getPlayUrl());
				vo.setPersonid(person.getId());
				vo.setIdCard(person.getIdCard());
				vo.setName(person.getName());
				vo.setSigntime(pmeSign.getSigntime());
				vo.setStatus(pmeSign.getState());
				vos.add(vo);
			}
		}
		return vos;
	}

	@Override
	public Long countZuoBanSignListV1(String phone, String personid) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PersonVo> getHuoDongPersonV1(String phone, String start, String size) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		StringBuffer sql = new StringBuffer(
				"select a from PMEPerson a,PMESign b where a.id=b.personid and b.signtypeid=3 and b.state=0 and a.status=0");
		List<PersonVo> voArray = new ArrayList<PersonVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(" and b.stbno='" + s.getNo() + "' order by b.signtime desc");
			} else {
				sql.append(" order by b.signtime desc");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMEPerson> personArray = persondao.findP(sql.toString(), Integer.parseInt(start),
					Integer.parseInt(size));
			for (PMEPerson p : personArray) {
				PersonVo vo = new PersonVo();
				BeanUtils.copyProperties(p, vo);
				vo.setImg("/resource/person/" + p.getIdCard() + ".png");
				voArray.add(vo);
			}
		}
		return voArray;
	}

	@Override
	public Long countHuoDongPersonV1(String phone) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SignVo> getHuoDongSignListV1(String phone, String personid, String start, String size)
			throws Exception {
		PMEPerson persons = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = persons.getArea();
		StringBuffer sql = new StringBuffer(
				"select a from PMESign a,PMESTB b where a.signtypeid=3 and a.stbno=b.no and a.state!=-1 and b.status=0 and a.personid="
						+ personid + "");
		List<SignVo> vos = new ArrayList<SignVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(" and b.no='" + s.getNo() + "' order by a.signtime desc");
			} else {
				sql.append(" order by a.signtime desc");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMESign> signArray = signdao.findP(sql.toString(), Integer.parseInt(start), Integer.parseInt(size));
			for (PMESign pmeSign : signArray) {
				PMEPerson person = persondao.get(PMEPerson.class, pmeSign.getPersonid());
				SignVo vo = new SignVo();
				vo.setPersonid(person.getId());
				vo.setIdCard(person.getIdCard());
				vo.setName(person.getName());
				vo.setSigntime(pmeSign.getSigntime());
				vo.setStatus(pmeSign.getState());
				vos.add(vo);
			}
			return vos;
		}
		return vos;
	}

	@Override
	public Long countHuoDongSignListV1(String phone, String personid) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PersonVo> getHuiYiPersonV1(String phone, String start, String size) throws Exception {
		PMEPerson person = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = person.getArea();
		StringBuffer sql = new StringBuffer(
				"select a from PMEPerson a,PMESign b where a.id=b.personid and b.signtypeid=1 and b.state=0 and a.status=0");
		List<PersonVo> voArray = new ArrayList<PersonVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(" and b.stbno='" + s.getNo() + "' order by b.signtime desc");
			} else {
				sql.append(" order by b.signtime desc");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMEPerson> personArray = persondao.findP(sql.toString(), Integer.parseInt(start),
					Integer.parseInt(size));
			for (PMEPerson p : personArray) {
				PersonVo vo = new PersonVo();
				BeanUtils.copyProperties(p, vo);
				vo.setImg("/resource/person/" + p.getIdCard() + ".png");
				voArray.add(vo);
			}
		}
		return voArray;
	}

	@Override
	public Long countHuiYiPersonV1(String phone) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SignVo> getHuiYiSignListV1(String phone, String personid, String start, String size) throws Exception {
		PMEPerson persons = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = persons.getArea();
		StringBuffer sql = new StringBuffer(
				"select a from PMESign a,PMESTB b where a.signtypeid=1 and a.stbno=b.no and a.state!=-1 and b.status=0 and a.personid="
						+ personid + "");
		List<SignVo> vos = new ArrayList<SignVo>();
		List<PMEArea> areas = areadao.find("from PMEArea where id=" + area.getId() + " and level=0");
		for (PMEArea a : areas) {
			PMESTB s = stbdao.get("from PMESTB where area.id='" + a.getId() + "'");
			if (s.getNo() != null && !"".equals(s.getNo())) {
				sql.append(" and b.no='" + s.getNo() + "' order by a.signtime desc");
			} else {
				sql.append(" order by a.signtime desc");
			}
			if (start == null || "".equals(start)) {
				start = "0";
			}
			if (size == null || "".equals(size)) {
				size = "10";
			}
			List<PMESign> signArray = signdao.findP(sql.toString(), Integer.parseInt(start), Integer.parseInt(size));
			for (PMESign pmeSign : signArray) {
				PMEPerson person = persondao.get(PMEPerson.class, pmeSign.getPersonid());
				SignVo vo = new SignVo();
				vo.setPersonid(person.getId());
				vo.setIdCard(person.getIdCard());
				vo.setName(person.getName());
				vo.setSigntime(pmeSign.getSigntime());
				vo.setStatus(pmeSign.getState());
				vos.add(vo);
			}
			return vos;
		}
		return vos;
	}

	@Override
	public Long countHuiYiSignListV1(String phone, String personid) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AreaVo> getAreaChildV1(String phone, String areapid, String start, String size) throws Exception {
		PMEPerson persons = persondao.get("from PMEPerson where phone='" + phone + "' and status=0");
		PMEArea area = persons.getArea();
		List<PMEArea> areaArr = new ArrayList<PMEArea>();
		System.out.println(area.getPid());
		if (start != null && !"".equals(start)) {
			areaArr = areadao.findP("from PMEArea where status=0 and pid=" + area.getId(), Integer.parseInt(start),
					Integer.parseInt(size));
		} else {
			areaArr = areadao.find("from PMEArea where status=0 and pid=" + area.getId());
		}
		List<AreaVo> vos = new ArrayList<AreaVo>();
		for (PMEArea pmeArea : areaArr) {
			PMEAreaCms ac = acdao.get("from PMEAreaCms where areakey='zcdw' and areaId=" + pmeArea.getId());
			AreaVo vo = new AreaVo();
			BeanUtils.copyProperties(pmeArea, vo);
			if (null != ac) {
				vo.setCmskey(ac.getCmsKey());
			}
			vos.add(vo);
		}
		return vos;
	}

	@Override
	public Long countAreaChildV1(String phone, String areapid) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
